package com.hw.IdGenerater;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;


/**
 * @author hewei8<br>
 * Date 2018年12月13日<br>
 * <p>唯一ID生成工具包</p>
 */
public class IDGenerater {

//	private static int[] numbers= {0,1,2,3,4,5,6,7,8,9};
//	private static char[] items= {0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e'};
	/*
	public static String transBytesToStr(byte[] bytes){
		StringBuffer buffer = new StringBuffer();
		for(int i = 0; i < bytes.length; i++){
			if(i != 0)
				buffer.append("-");
			//bytes[i]&0xff将有符号byte数值转换为32位有符号整数，其中高24位为0，低8位为byte[i]
			int intMac = bytes[i]&0xff;
			//toHexString函数将整数类型转换为无符号16进制数字
			String str = Integer.toHexString(intMac);
			if(str.length() == 0){
				buffer.append("0");
			}
			buffer.append(str);
		}
		return buffer.toString().toUpperCase();
	}
	*/
	/*获取计算机Mac地址
	InetAddress adress = InetAddress.getLocalHost();
	NetworkInterface net = NetworkInterface.getByInetAddress(adress);
	byte[] macBytes = net.getHardwareAddress();
	System.out.println(transBytesToStr(macBytes));
	*/
	
	/**
	 * @return String<br>
	 * <p>获取原生的UUID</p>
	 */
	public synchronized String getOriginalUUID() {
		return UUID.randomUUID().toString();
	}
	
	/**
	 * @return String<br>
	 * <p>获取没有分隔符的UUID</p>
	 */
	public synchronized String getStrUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
	
	/**
	 * @param max
	 * @return int<br>
	 * <p>获得0-max之间（不包括max）的随机数</p>
	 */
	public synchronized int getRandomID(int max) {
		Random random=new Random();
		return random.nextInt(max);
	}
	
	/**
	 * @return String<br>
	 * <p>获得当前时间戳</p>
	 */
	public String getTimestamp() {
		SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return sdf.format(new Date());
	}
	
	/**
	 * @param length
	 * @return String<br>
	 * <p>获得length长度的随机数</p>
	 */
	public String getRandNum(int length) {
		Random random=new Random();
		StringBuffer str=new StringBuffer();
		for(int i=0;i<length;i++) {
//			int seq=random.nextInt(10);
//			str.append(numbers[seq]);
			str.append(random.nextInt(10));
		}
		return str.toString();
	}
	
	/**
	 * @param length
	 * @return String<br>
	 * <p>
	 * 获得的唯一ID<br/>
	 * ID=当前时间+length长度的随机数
	 * </p>
	 */
	public synchronized String getTimeAndRandomID(int length) {
		return getTimestamp()+getRandNum(length);
	}
	
	
	
}
